﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JustTank
{
    public class TankHuman : Tank
    {
        public new const string CollisionGroupString = "playerTank";
        public ConsoleColor Color = ConsoleColor.White;

        private bool shoot;

        public TankDirection Direction {get; set;}



        public TankHuman(MatrixCoords topLeft)
            : base(topLeft)
        {
            this.shoot = false;
        }        

        public void MoveLeft()
        {
            this.topLeft.Col--;
        }

        public void MoveRight()
        {
            this.topLeft.Col++;
        }

        public void MoveUp()
        {
            this.topLeft.Row--;
        }

        public void MoveDown()
        {
            this.topLeft.Row++;
        }

        public void ShootBullet()
        {
            this.shoot = true;
        }

        public override string GetCollisionGroupString()
        {
            return TankHuman.CollisionGroupString;
        }

        public override bool CanCollideWith(string otherCollisionGroupString)
        {
            return otherCollisionGroupString == "block" || otherCollisionGroupString == "botBullet";
        }

        public override void RespondToCollision()
        {
            this.IsDestroyed = true;
        }

        protected override void UpdatePosition()
        {
            this.TopLeft += this.Speed;
            this.body = GetTankBody(GetTankDirection(Speed));
        }

        public override void Update()
        {
            this.body = GetTankBody(this.Direction);
        }

        public override IEnumerable<GameObject> ProduceObjects()
        {
            List<PlayerBullet> bullets = new List<PlayerBullet>();
            if (this.shoot == true)
            {
                if (this.Direction == TankDirection.Up)
                {
                    bullets.Add(new PlayerBullet(new MatrixCoords(this.topLeft.Row, this.topLeft.Col), this.Direction));
                }
                if (this.Direction == TankDirection.Left)
                {
                    bullets.Add(new PlayerBullet(new MatrixCoords(this.topLeft.Row, this.topLeft.Col), this.Direction));
                }
                if (this.Direction == TankDirection.Right)
                {
                    bullets.Add(new PlayerBullet(new MatrixCoords(this.topLeft.Row, this.topLeft.Col + 1), this.Direction));
                }
                if (this.Direction == TankDirection.Down)
                {
                    bullets.Add(new PlayerBullet(new MatrixCoords(this.topLeft.Row + 1, this.topLeft.Col), this.Direction));
                }
                
                this.shoot = false;
            }
            
            return bullets;
        }
    }
}
